package com.yonyou.brigade.facade.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.facade.model.ChannelDTO;
import com.yonyou.brigade.facade.service.IOpenService;
import com.yonyou.ybzboot.starter.common.result.BasicResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商旅开通
 *
 * @author kangfw5
 * @since : 2018-09-18
 */
@RestController
@RequestMapping("/open")
public class OpenController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("openServiceImpl")
    private IOpenService openService;

    @Autowired
    private IBriChannelService briChannelService;

    /**
     * 查询第三方商旅服务开通情况
     * @return
     */
    @RequestMapping(value = "/api/v1/openStateList", method = RequestMethod.GET)
    public JSONObject openStateList(HttpServletRequest request) {
        JSONObject result = openService.openStateList(request);
        return result;
    }

    /**
     * 查询开通第三方商旅服务配置模板信息
     * @return
     */
    @RequestMapping(value = "/api/v1/queryConfig", method = RequestMethod.POST)
    public JSONObject queryConfig(@RequestBody JSONObject params) {
        JSONObject result = openService.queryConfig(params);
        return result;
    }

    /**
     * 第三方商旅服务开通配置信息更新与保存
     * @return
     */
    @RequestMapping(value = "/api/v1/saveConfig", method = RequestMethod.POST)
    public JSONObject saveConfig(@RequestBody JSONObject params) {
        JSONObject result = openService.saveConfig(params);
        return result;
    }

    /**
     * 第三方商旅服务关闭
     * @return
     */
    @RequestMapping(value = "/api/v1/closeChannel", method = RequestMethod.POST)
    public JSONObject closeChannel(@RequestBody JSONObject params) {
        JSONObject result = openService.closeChannel(params);
        return result;
    }

    /**
     * @param params tenantid:租户id
     * @date: 2018/9/20 20:15
     * @Description: 友商旅（中航易购）开通
     */
    @RequestMapping(value = "/rtpnr", method = RequestMethod.POST)
    public Map openBrigade(@RequestBody Map<String, String> params) {
        String tenant = params.get("tenantId");
        return openService.openBrigade(tenant);
    }

    /**
     * 商旅服务开通
     *
     * @since 2019-02-18
     */
    @RequestMapping(value = "/open", method = RequestMethod.POST)
    public BasicResult open(@RequestBody @Valid ChannelDTO channelDTO) {
        return openService.open(channelDTO);
    }

    /**
     * 商旅服务开通信息查询
     *
     * @since 2019-03-11
     */
    /*@RequestMapping(value = "/open", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryOpenList(String tenantId, String userId) {

    }*/

    /**
     * @param params tenantid:租户id
     * @Description: 查询租户开通的第三方商旅服务
     */
    @RequestMapping(value = "/queryThird/isOpen", method = RequestMethod.POST)
    public JSONObject thirdIsOpen(@RequestBody JSONObject params) {
        String tenant = params.getString("tenantId");
        List<BriChannel> briChannels = briChannelService.getEnableChannelByTenant(tenant);
        List<String> channels = new ArrayList<>();
        if (!CollectionUtils.isEmpty(briChannels)) {
            for (BriChannel briChannel : briChannels) {
                channels.add(briChannel.getChannel());
            }
        }
        params.clear();
        params.put("success", true);
        params.put("thirdOpen", JSON.toJSONString(channels));
        return params;
    }
}
